package com.ezjie.ielts.module_word.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ezjie.ielts.R;
import com.ezjie.ielts.model.Mean;
import com.ezjie.ielts.model.Question;

public class ReviewChooseAdapter extends BaseAdapter {

	private Context mContext;
	private List<Question> list;

	public ReviewChooseAdapter(Context mContext) {
		this.mContext = mContext;
	}

	public void setList(List<Question> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list == null ? 0 : list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder hv;
		if (view == null) {
			view = LayoutInflater.from(mContext).inflate(R.layout.review_choose_item, null);
			hv = new ViewHolder();
			hv.instance = (TextView) view.findViewById(R.id.instance);
			hv.line = view.findViewById(R.id.list_line);
			hv.tv_ll = (LinearLayout) view.findViewById(R.id.tv_ll);
			hv.select_num = (TextView) view.findViewById(R.id.select_num);
			hv.fooder_line = view.findViewById(R.id.fooder_list_line);
			view.setTag(hv);
		} else {
			hv = (ViewHolder) view.getTag();
		}

		if (list.get(position).isIs_right()) {
			hv.tv_ll.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_type_right));
		} else {
			hv.tv_ll.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_type_wrong));
		}

		if (position == 0) {
			hv.line.setVisibility(View.VISIBLE);
		}
		if (position == list.size() - 1) {
			hv.fooder_line.setVisibility(View.GONE);
		} else {
			hv.fooder_line.setVisibility(View.VISIBLE);
		}
		List<Mean> means = list.get(position).getSelection_text();
		if (means != null) {
			for (int i = 0; i < means.size(); i++) {
				hv.select_num.setText(list.get(position).getSelect_num() + ".");
				hv.instance.setText(means.get(i).getPos() + means.get(i).getText());
			}
		}
		return view;
	}

	class ViewHolder {
		TextView instance, select_num;
		View line, fooder_line;
		LinearLayout tv_ll;
	}
}
